import React, { ReactNode } from 'react'

import { CacheType } from '@/global/constant'
import { localCache } from '@/utils/cache'
import { Navigate } from 'react-router-dom'

interface IProps {
  children?: ReactNode
}

const AuthRoute = (props: IProps) => {
  const token = localCache.getCache(CacheType.TOKEN)
  // 如果token存在 直接正常渲染
  if (token && token.tokenValue) {
    return <>{props.children}</>
  }
  // 如果token不存在，重定向到登录路由
  else {
    return <Navigate to="/login" replace />
  }
}

export default AuthRoute
